<?php

/**
 * This is the model class for table "tbl_order".
 *
 * The followings are the available columns in table 'tbl_order':
 * @property integer $id
 * @property string $hash
 * @property string $username
 * @property string $phone
 * @property string $email
 * @property integer $is_new
 * @property string $created_at
 * @property string $address
 */
class Order extends CActiveRecord
{
    public $verifyCode;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_order';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, phone', 'required', 'message' => '{attribute} не может быть пустым.'),
			array('hash', 'unique', 'className'=>'Order'),
			array('email', 'email', 'message'=>'Указан неверный формат адреса электронной почты.'),
			array('is_new', 'numerical', 'integerOnly'=>true),
			array('hash, username, phone, email', 'length', 'max'=>128),
            array('products, address, comment', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, hash, username, phone, email, is_new, created_at, address, comment', 'safe', 'on'=>'search'),
            array(
                'verifyCode',
                'captcha',
                'message' => 'Неправильный код проверки.',
                // авторизованным пользователям код можно не вводить
                'allowEmpty'=>!Yii::app()->user->isGuest || !CCaptcha::checkRequirements(),
            ),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'productRel'=>array(self::HAS_MANY,'OrderProduct','order_id'),
            'products'=>array(self::HAS_MANY,'Product', 'product_id', 'through'=>'productRel'),

		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'hash' => 'Hash',
			'username' => 'Имя',
			'phone' => 'Телефон',
			'email' => 'Email',
			'is_new' => 'Выполнен',
			'created_at' => 'Дата',
            'address' => 'Адрес',
            'verifyCode'=>'Код проверки',
            'comment'=>'Комментарий'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('hash',$this->hash,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('is_new',$this->is_new);
		$criteria->compare('created_at',$this->created_at,true);
        $criteria->compare('address',$this->address,true);
        $criteria->compare('comment',$this->address,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Order the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}


}
